<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Block_Core {

	protected $_template = '';
	protected $_childs = array();
	protected $_data = array();
	
	public function __construct() { }
	
	/* core */
	public function setTemplate($template) {
		$this->_template = $template;
	}
	
	public function getTemplate() {
		return $this->_template;
	}
	
	public function setChild($name, $block) {
		if (is_object($block)) $this->_childs[$name] = $block;
	}
	
	public function getChild($name) {
		if (!array_key_exists($name, $this->_childs)) return null;
		return $this->_childs[$name];
	}

	public function render() {
		$tpl = $this->getTemplate();
		if ($tpl=='') {
			foreach ($this->_childs as $child)
				$child->render();
		} else {
			// pass current block & helper to view
			$data = array('block' => $this);	
			App::view(App::template($tpl), $data);
		}
		return $this;
	}
	
	public function getData($name) {
		return $this->_data[$name];
	}
	
	public function setData($name, $value) {
		$this->_data[$name] = $value;
	}
	
	/* render child block */
	public function renderChild($name='') {
		if ($this->getChild($name)) {
			$this->getChild($name)->render();
		} else {	// render all child
			foreach($this->_childs as $child)
				$child->render();
		}
		return $this;
	}
	
	/* translate text */
	public function text($str) {
		return $str;
	}
}

/* End of file Core.php */